package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyUtils {
	private static Properties prop;  
    
    public static void load(String path){  
        //�����path����Ŀ�ļ��ľ���·��  
        //�Ȼ�ȡ��Ŀ����·����Thread.currentThread().getContextClassLoader().getResource("").getPath();  
        //Ȼ������Ŀ·������ƴ��"properties/sysConfig.properties";  
        prop= new Properties();// ���Լ��϶���     
        FileInputStream fis = null;    
        try {    
            System.out.println(path);  
            fis = new FileInputStream(path);  
            prop.load(fis);  
        } catch (FileNotFoundException e) {    
            e.printStackTrace();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }finally{
        	try {
        		if(null!=fis){
        			fis.close();
        		}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// �ر���    
        }   
    }  
      
    //����ΪҪ�޸ĵ��ļ�·��  �Լ�Ҫ�޸ĵ�������������ֵ    
    public static Boolean updatePro(String path,String key,String value){  
        if(prop==null){  
            load(path);  
            System.out.println("�޸�ǰ���¼���һ��");  
        }  
        System.out.println("��ȡ��ӻ��޸�ǰ������ֵ��"+key+"=" + prop.getProperty(key));  
        prop.setProperty(key, value);    
        FileOutputStream fos = null;
        // �ļ������     
        try {    
        	fos = new FileOutputStream(path);     
            // ��Properties���ϱ��浽����     
            prop.store(fos, "Copyright (c) Boxcode Studio");     
            
        } catch (FileNotFoundException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
            return false;    
        } catch (IOException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
            return false;    
        }finally{
        	try {
        		if(null!=fos){
        			fos.close();
        		}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// �ر���     
        }   
        System.out.println("��ȡ��ӻ��޸ĺ������ֵ��"+key+"=" + prop.getProperty(key));  
        return true;    
    }    
      
  //����ΪҪ�޸ĵ��ļ�·��  �Լ�Ҫ�޸ĵ�������������ֵ    
    public static String getPro(String path,String key){    
        if(prop==null){  
            load(path);  
            System.out.println("���¼���һ��");  
        }  
        FileInputStream fis = null;    
        try {    
            fis = new FileInputStream(path);    
            prop.load(fis);// �������ļ���װ�ص�Properties������     
        } catch (FileNotFoundException e) {    
            e.printStackTrace();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }finally{
        	try {
        		if(null!=fis){
        			fis.close();
        		}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// �ر���    
        }      
        System.out.println("��ѯ����"+key+"��ֵ��"+prop.getProperty(key));  
        return prop.getProperty(key);    
    }    
    
    public static void main(String[] args) {
    	String path = PathUitls.getFilePathOfResource("all.properties");
    	String vaule = PropertyUtils.getPro(path, "jdbc.url");
    	System.out.println(vaule);// /jdbc.properties
	}
}
